package com.psg.controller;

import com.psg.pojo.Order;
import com.psg.service.CarService;
import com.psg.service.OrderService;
import com.psg.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController{


    @Autowired
    private OrderService orderService;
    @Autowired
    private CarService carService;



    @RequestMapping("/buy")
    @ResponseBody
    public Result buy(Integer cid, String shortName, Integer num, Float total, HttpServletRequest request, HttpServletResponse response){
        try {
            if(cid!=null){
                carService.deleteCar(cid);
            }
            Order order = new Order();
            order.setOrder_id(getUserId());
            order.setPname(shortName);
            order.setNum(num);
            order.setPrice(Double.valueOf(total));
            order.setOstatus("已支付");
            orderService.insertOrder(order);
            return Result.success();
        }catch(Exception e){
            e.printStackTrace();
            return Result.error("支付失败！");
        }
    }

}
